home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 October / Chip_2004-10_cd1.bin / tema / where / Setup.exe / {app} / DescAPI / Sample / SampleModule.dpr < prev   
Encoding:
Text File  |  2000-08-31  |  5.4 KB  |  151 lines

  1. //============================================================================
  2. //                               SampleModule.DLL
  3. //               WhereIsIt Description API 2.0 Demontration Module
  4. //                         Robert Galle, July 2000
  5. //============================================================================
  6.  
  7. {$I-}
  8.  
  9. library SampleModule;
  10.  
  11. uses Windows;
  12.  
  13. {$DEFINE DescAPI_20}
  14.  
  15. {$INCLUDE DescAPI.inc}
  16.  
  17.  
  18. //****************************************************************************
  19. //                 Implementation of "Text file import" plugin
  20. //****************************************************************************
  21.  
  22. procedure ImportTextFile(const FileList, DescResult: PChar; RequireFileProc: TRequireFileProc);
  23.  
  24. // Plugin will import the first 5 lines of text from first specified description
  25. // file and use it as a description for parent item (folder or archive file).
  26.  
  27. // NOTE: This is a VERY simplified plugin. It does nothing special, and does
  28. // not include any special data checking or error recovery apart from the
  29. // most basic one. It should only be used as reference to Description API
  30. // implementation, not as a base for any "real" plugins.
  31.  
  32. const NumLines = 5;   // number of lines to import
  33.  
  34. var StoreFileMode, n: Integer;
  35.     TextFile: Text;
  36.     TextFilePath: PChar;
  37.     Line,Desc: string;
  38. begin
  39.   // FileList points here to a list of found files matching plugin's file mask,
  40.   // not in any particular order. This list is actually a single stream of
  41.   // null-terminated strings, attached to each other, and an empty string
  42.   // ending the list, for example:
  43.   //  'README.TXT'#0'DESC.TXT'#0'DESC.NFO'#0'LICENSE.TXT'#0#0
  44.   // Each plugin should decide what to do with found files, and which one to
  45.   // use for importing. This sample plugin will just import the first file in
  46.   // the list.
  47.  
  48.   // Let's request the first file in the list from WhereIsIt
  49.   TextFilePath:=RequireFileProc(FileList);
  50.  
  51.   // plugin will open file as read-only, so opening should not fail even if
  52.   // file is already opened by some other program.
  53.   StoreFileMode:=FileMode;
  54.   FileMode:=0;   // read-only
  55.  
  56.    // try to open the file
  57.   AssignFile(TextFile,TextFilePath);
  58.   Reset(TextFile);
  59.  
  60.    // if something went wrong with opening the file, just leave the description
  61.   // alone and exit
  62.   if IOResult<>0 then Exit;
  63.  
  64.   try
  65.     // read the first <NumLines> and store them in Desc
  66.     n:=0;
  67.     Desc:='';
  68.     while not EOF(TextFile) and (n<NumLines) do begin
  69.       ReadLn(TextFile,Line);
  70.       Desc:=Desc+#13#10+Line;
  71.       Inc(n);
  72.     end;
  73.     // copy the description to where it is expected (DescResult buffer),
  74.     // but make sure not to exceed the maximum description buffer size
  75.     // specified with MaxDescLength constant!
  76.     LStrCpyN(DescResult,PChar(Desc),MaxDescLength-1);
  77.   finally
  78.     // just clean up an return
  79.     Close(TextFile);
  80.     FileMode:=StoreFileMode;
  81.   end;
  82. end;
  83.  
  84.  
  85. //****************************************************************************
  86. //                      Description API 2.0 interface
  87. //****************************************************************************
  88.  
  89. procedure ModuleInfoEx(ModuleInfo: TModuleInfoPtr); stdcall;
  90. begin
  91.   // just provides some basic information about description module
  92.   with ModuleInfo^ do begin
  93.      LStrCpy(lpszModuleName, 'WhereIsIt Sample Description Module');
  94.      LStrCpy(lpszAuthor, 'Robert Galle');
  95.      LStrCpy(lpszVersion, '2.0');
  96.    end;
  97. end;
  98.  
  99. procedure ConfigureProc(HInstance: THandle; OwnerWnd: THandle; PluginID: Word); stdcall;
  100. begin
  101.   // this one executes when user requests to configure plugin. In this sample
  102.   // it will just show a simple dialog with version information.
  103.   MessageBox(OwnerWnd,
  104.     PChar('Sample Description Module v2.0'+#13#13'Here could be implemented a '+
  105.           'special dialog where users could configure the plugin.'),
  106.     'About SampleModule.DLL',
  107.     MB_IconInformation+MB_OK);
  108. end;
  109.  
  110. procedure RegisterDescPlugins(RegisterPlugin: TRegisterPlugin); stdcall;
  111. begin
  112.    // register the plugin in WhereIsIt
  113.    RegisterPlugin(0, ptParentItem, 'Text files import', '*.TXT,READ.ME', ConfigureProc);
  114. end;
  115.  
  116. function ImportDesc_ParentItemEx(PluginID: Word; ImportParentItem: TImportParentItemPtr): Integer; stdcall;
  117. begin
  118.   // This procedure is called for each ptParentItem plugin in this module. It
  119.   // should check requested PluginID, and run the appropriate plugin. Available
  120.   // for plugins are a list of found description files, matching plugin's
  121.   // FileMask (FoundList parameter), and address of buffer where the extracted
  122.   // description is expected.
  123.  
  124.   // assume that description will be an empty string by default
  125.   ImportParentItem.Desc[0]:=#0;
  126.   // call appropriate plugin by the PluginID - not much choice, we only have
  127.   // one plugin in this sample
  128.   IOResult;
  129.   Result:=0;
  130.   with ImportParentItem^ do
  131.      case PluginID of
  132.        0: if FoundList>'' then ImportTextFile(FoundList,Desc,RequireFileProc)
  133.           else Result:=-1
  134.        else Result:=-2;
  135.      end;
  136.   // function should return a negative value if description was not retrieved
  137.   // successfully, for whatever reason.
  138.   if (ImportParentItem.Desc='') then Result:=-1;
  139. end;
  140.  
  141.  
  142. // The DLL library needs to export functions required by Description API 2.0
  143. exports
  144.   ModuleInfoEx,
  145.   RegisterDescPlugins,
  146.   ImportDesc_ParentItemEx;
  147.  
  148. // That's it. Simple, eh?
  149.  
  150. end.
  151.